package com.elaine.factory.alapi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.elaine.constant.AlapiConstant;
import com.elaine.model.alapi.AcgData;
import com.elaine.model.alapi.AlapiResponse;
import com.elaine.utils.HttpUtils;
import org.springframework.stereotype.Component;

/**
 * @author wrb
 * @date 2020/4/1 15:41
 */
@Component
public class AcgAlapiFactory extends AlapiFactory {

	private static final String URL = "https://v1.alapi.cn/api/acg?format=json";

	@Override
	protected void registerFactory() {
		doRegister(AlapiConstant.ACG, this);
	}

	@Override
	public String getReply() {
		String jsonStr = HttpUtils.requestGet(URL, null, null);
		AlapiResponse<AcgData> alapiResponse = JSON.parseObject(jsonStr, new TypeReference<AlapiResponse<AcgData>>() {
		});
		if (alapiResponse != null && alapiResponse.isSuccess()) {
			String url = alapiResponse.getData().getUrl();
			return super.getImageMediaIdByKey(url.substring(url.lastIndexOf("/") + 1), url, AlapiConstant.ACG.name());
		} else {
			throw new IllegalArgumentException("ACG图片请求失败咯，wrwrwr...");
		}
	}
}

